package com.jsmile.cloud.accountcenter.shop.repo;

import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantQuery;
import com.jsmile.cloud.accountcenter.shop.dao.TenantDao;
import com.jsmile.cloud.accountcenter.shop.model.Tenant;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:39:24
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class TenantSearchRepository extends ServiceImpl<TenantDao, Tenant> {

    public IPage<Tenant> pageQuery(ReqTenantQuery reqTenantQuery) {
        return this.baseMapper.selectPage(reqTenantQuery.getPage(), getQueryWrapper(reqTenantQuery));
    }

    public Wrapper getQueryWrapper(ReqTenantQuery reqTenantQuery) {
        return new QueryWrapper<Tenant>().lambda().eq(null != reqTenantQuery.getEnable(), Tenant::getEnable, reqTenantQuery.getEnable())
            .like(Objects.nonNull(reqTenantQuery.getTenantNo()), Tenant::getTenantNo, reqTenantQuery.getTenantNo())
            .like(Objects.nonNull(reqTenantQuery.getContactName()), Tenant::getContactName, reqTenantQuery.getContactName())
            .like(Objects.nonNull(reqTenantQuery.getTenantName()), Tenant::getTenantName, reqTenantQuery.getTenantName())
            .like(Objects.nonNull(reqTenantQuery.getContactPhone()), Tenant::getContactPhone, reqTenantQuery.getContactPhone())
            .ge(null != reqTenantQuery.getStartDate(), Tenant::getCreateTime, reqTenantQuery.getStartDate())
            .le(null != reqTenantQuery.getEndDate(), Tenant::getCreateTime, reqTenantQuery.getEndDate()).orderByDesc(Tenant::getCreateTime);// sort,createTime倒序
    }
}
